package cs2340.klondike.TaskFudger;

import java.sql.SQLException; 

import models.User;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

import db.DatabaseHelper;

import models.*;

/**
 * This class creates a new category if the Create Button is clicked.
 * Allows the user to give a name and description for the new category.
 */
public class NewCategoryActivity extends OrmLiteBaseActivity<DatabaseHelper> {
	private Button saveButton;
	private Button cancelButton;
	private Button deleteButton;
	private EditText nameField;
	private EditText descriptionField;
	private TextView nameLabel;
	private boolean edit;
	private Category thisCategory;
	
	/* (non-Javadoc)
	 * @see com.j256.ormlite.android.apptools.OrmLiteBaseActivity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_category);
		
		saveButton = (Button)findViewById(R.id.createCategory);
        cancelButton = (Button)findViewById(R.id.cancelCreateCategory);
        nameField = (EditText)findViewById(R.id.categoryName);
        descriptionField = (EditText)findViewById(R.id.categoryDescription);
        deleteButton = (Button)findViewById(R.id.deleteCategory);
        
        nameLabel = (TextView)findViewById(R.id.categoryNameLabel);
        
        deleteButton.setText("Delete");
        cancelButton.setText("Cancel");
		
		cancelButton.setOnClickListener(new Button.OnClickListener()
        {
        	public void onClick(View v)
        	{
        		Intent intent = new Intent(v.getContext(), TabsActivity.class); 
        		intent.putExtra("TAB_NUMBER", 1);
        		startActivity(intent);
        	}
        });
		
		if(this.getIntent().hasExtra("CATEGORY_ID")){
			edit = true;
			thisCategory = Category.getDefaultDao().queryForId(this.getIntent().getIntExtra("CATEGORY_ID", 0));
			saveButton.setText("Update Category");
			nameField.setText(thisCategory.getName());
			descriptionField.setText(thisCategory.getDescription());
			Log.d("EDIT CATEGORY",thisCategory.getName());
 
			deleteButton.setEnabled(true);
	        deleteButton.setVisibility(View.VISIBLE);
	        saveButton.setText("Update");
	        	
        	deleteButton.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					try{
						thisCategory.delete();
						User.getCurrentUser().refresh();
					}
					catch(Exception e){
						e.printStackTrace();
					}
	        			Intent intent = new Intent(v.getContext(), TabsActivity.class); 
		        		intent.putExtra("TAB_NUMBER", 1);
		        		startActivity(intent);
		        		finish();
				}
			});

		}
		
		else{
			edit = false;
			Log.d("CREATE","new");
			saveButton.setText("Create Category");
			deleteButton.setEnabled(false);
	        deleteButton.setVisibility(View.GONE);

		}
		
		saveButton.setOnClickListener(new Button.OnClickListener()
        {
        	public void onClick(View v)
        	{   
        		boolean validInfo = true;
        		
        		
        		if(nameField.getText().toString().length() <= 0)
        		{
        			validInfo = false;
        			nameLabel.setText("Name Required");
        			nameLabel.setTextColor(Color.RED);
        		}
        		
        		else
        		{
        			nameLabel.setText("Name");
        			nameLabel.setTextColor(Color.WHITE);
        		}
        		
        		if(validInfo == true)
        		{	        			
        			if(edit){
        				
        				try{
        					thisCategory.delete();
        				}catch (SQLException e1) {
        						// TODO Auto-generated catch block
        						e1.printStackTrace();
        				}
        				
        				
        			}
        			
        			if(!edit){
	        			thisCategory = new Category();
        			}	
        			
	        		thisCategory.setName(nameField.getText().toString());
	        		thisCategory.setDescription(descriptionField.getText().toString());
	        		        			
        			if(!edit){
        				thisCategory.setUser(User.getCurrentUser());
        			}
    			
        			try 
        			{
        				thisCategory.create();
						
						// Collection will need to be reloaded
        				thisCategory.getUser().refresh();
					} 
        			catch (SQLException e)
        			{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
        			
        			Intent intent = new Intent(v.getContext(), TabsActivity.class); 
	        		intent.putExtra("TAB_NUMBER", 1);
            		startActivity(intent);
        		}
        	
        		
           	}
        });
	}
}
